  var mobileValidate = false
  var imgcodeValidate = false
  var phonecodeValidate = false
  var t; //倒计时间
  var timer; //定时器
  //手机验证码倒计时
  function timeOut() {
    t -= 1;
    if (t != 0) {
      $('.count-time').text(t + "s");
    } else {
      $(".get-phone-code").show().siblings('.count-time').hide();
      return;
    }
    //每秒执行一次,showTime()
    timer = setTimeout(timeOut, 1000);
  }

  // 登录
  $('.login-btn').click(function(){
    checkMobileNo()
    checkMobileCode()
    checkImgCode()

    if (mobileValidate && imgcodeValidate && phonecodeValidate) {
      if ($(".login-btn").hasClass("login-disable")) {
        return false;
      } else {
        $(".login-btn").addClass("login-disable");
      }

      var mobile = $("#mobileNum").val();
      var mobileCode = $("#mobileCode").val();
      quickLogin(mobile, mobileCode, function (data) {
        if (data.success) {
          var sso_token = {
            token: data.responseObject,
            time_stamp: parseInt(new Date().getTime() / 1000)
          }
          localStorage.SSO_TOKEN = JSON.stringify(sso_token)
          $(".login-btn").removeClass("login-disable")
          setTimeout(function(){
            $(".login-box").hide();
          }, 1500)
        } else if (data.errorInfo == "The validate code is invalid") {
          showMessage(false, "#mobileCode", "短信验证码错误或已无效,请重新获取验证码!");
          $(".login-btn").removeClass("login-disable");
        } else if (!data.success && data.errorInfo == '短信验证码错误') {
          showMessage(false, "#mobileCode", "短信验证码错误,请刷新页面重新操作!");
          $(".login-btn").removeClass("login-disable");
        } else {
          alert("十分抱歉,网络异常,请刷新重新输入!");
          $(".login-btn").removeClass("login-disable");
        }
      })
    }
  })

  $('.input-wrap input').focus(function () {
    showMessage(true, this, ' ')
  })

  // 手机号码检测
  $('#mobileNum').blur(function () {
    checkMobileNo()
  })
  // 图形验证码检测
  $('#imgCode').blur(function () {
    checkImgCode()
  })
  // 短信验证码检测
  $("#mobileCode").blur(function () {
    checkMobileCode()
  })

  // 刷新图形验证码
  $('.input-wrap').on('click', '.img-code', getImgCode)

  // 获取验证码
  $('.get-phone-code').click(function(){
    if (!mobileValidate) {
      showMessage(mobileValidate, "#mobileNum", '手机号不能为空!');
      return;
    }
    if (!imgcodeValidate) {
        showMessage(imgcodeValidate, "#imgCode", '图形验证码不能为空!');
        return;
    }
    var mobile = $("#mobileNum").val();
    var imgCode = $("#imgCode").val();
    var isPass = 0;
    getOperationVCode(mobile, imgCode, function(data){
      if (data.validateCodeResult == false) {
        showMessage(false, "#mobileCode", "验证码错误,请核对后重新输入!");
        getImgCode();
      } else {
        isPass = 1;
        if (data.success == true) {
          showMessage(false, "#mobileCode", "验证码已发送,请查看手机短信!");
          if (isPass == 1) {
            t = 60;
            $('.get-phone-code').hide().siblings('.count-time').show();
            timeOut();
          }
        } else if(!data.success && data.errorInfo == '3分钟内已发送过短信,请稍后再试'){
          showMessage(false, "#mobileCode", '验证码3分钟内有效');
        } else { // 图形验证码错误
          showMessage(false, "#imgCode", data.errorInfo);
          getImgCode()
        }
      }
    })
  })


  // 登录框关闭
  $('.login-close-btn').click(function(){
    $(this).parents('.login-box').hide()
  })

  // 是否有登录
  function isLogin(callback){
    if (localStorage.SSO_TOKEN) {
      var local_storage = JSON.parse(localStorage.SSO_TOKEN);
      var now_stamp = parseInt(new Date().getTime() / 1000);
      var time_min = parseInt(now_stamp - local_storage.time_stamp) / 3600;
      if (time_min < 0.5) {
        var token = local_storage.token;
        // console.log(window.userInfo)
        // callback(token)
        if (userInfo.subscribe) {
          callback(token)
        } else {
          $('.qr').show().siblings('.tip-content').hide().siblings('.tip-bg').hide().parent('.tip-box').show()
          return false
        }
      } else {
        localStorage.removeItem('SSO_TOKEN');
        $('.login-box').show()
      }
    } else {
      localStorage.removeItem('SSO_TOKEN');
      $('.login-box').show()
    }
  }

  /**验证手机号码**/
  function checkMobileNo() {
    var mobile = $("#mobileNum").val();
    var message;
    if (mobile == "") {
      message = "手机号不能为空!"
      mobileValidate = false
    } else if (mobile.length != 11) {
      message = " "
      mobileValidate = false
    } else{
      mobileValidate = true
      getImgCode()
    }
    showMessage(mobileValidate, "#mobileNum", message);
  }

  // 验证图形验证码
  function checkImgCode() {
    if (mobileValidate) {      
      var imgCode = $("#imgCode").val();
      var message = "";
      if (imgCode == "") {
        message = "图形验证码不能为空!";
        imgcodeValidate = false
      } else {
        imgcodeValidate = true
      }
      showMessage(imgcodeValidate, "#imgCode", message);
    } else {
      showMessage(true, "#imgCode", '请输入图形验证码');
    }
  }

  // 验证短信验证码
  function checkMobileCode() {
    var mobileCode = $("#mobileCode").val();
    var message = "";
    if (imgcodeValidate) {      
      if (mobileCode == "") {
        message = "短信验证码不能为空!";
        phonecodeValidate = false
      } else if (mobileCode == null) {
        message = "短信验证码已过时,请重新获取验证码";
        phonecodeValidate = false
      } else {
        phonecodeValidate = true;
      }
      showMessage(phonecodeValidate, "#mobileCode", message);
    } else {
      showMessage(true, "#mobileCode", '请输入短信验证码');
    }
  }

  // 获取图形验证码
  function getImgCode() {
    var mobileNum = $('#mobileNum').val()
    $('.img-code').show().find('img').attr('src', 'http://activity.gzl.com.cn/v2/user/getValidateCode?storeKey=' + mobileNum + '&v=' + Math.random())
  }

  // 获取短信验证码
  function getOperationVCode(quickMobileNo, validateCodeNew, callback) {
    $.ajax({
      type: "GET",
      url: "http://activity.gzl.com.cn/v2/user/getOperationVCode?mobiNum=" + quickMobileNo + "&imgVCode=" + validateCodeNew + "&channel=weixin",
      async: false, //同步
      success: function (data) {
        callback(data);
      },
      error: function (data) {
        console.log('getmobile-error:', data)
        alert("抱歉,网络异常,请重试。");
      }
    })
  }

  //确认登录
  function quickLogin(quickMobileNo, mobileCode, callback) {
    $.ajax({
      type: "POST",
      url: "http://activity.gzl.com.cn/v2/user/quickLogin", // + quickMobileNo + "&vCode=" + mobileCode + "&channel=B2C"
      data: {
        "mobiNum": quickMobileNo,
        "channel": "weixin",
        "vCode": mobileCode,
      },
      async: true,
      success: function (data) {
        callback(data);
      },
      error: function (err) {
        alert("十分抱歉,网络异常,请刷新重新输入!");
        $(".login-btn").removeClass("login-disable")
      }
    });
  }

  function showMessage(isRight, id, message){
    if (!isRight) {
      $(id).siblings('.placehold-tip').text(message).parent('.input-wrap').addClass('err-tip')
    } else {
      $(id).siblings('.placehold-tip').text(message).parent('.input-wrap').removeClass('err-tip')
    }
  }
